Page({
  data: {
    dateLayerVisible: false,
    houseLayerVisible: false,
    houseList: [
      // { name: '北京西三旗花园1号楼 101' },
      // { name: '北京东村家园3号楼 302' },
      // { name: '北京育新花园3号楼 703' },
      // { name: '北京天通苑北苑8号楼 403' },
    ],
    name: '',
    mobile: '',
    gender: 1,
    currentDate: Date.now(),
  },
  onLoad() {
    this.getHouseList()
  },
  getTime(ev) {
    // console.log(ev.detail)
    // wx.utils.getStringFromTime(ev.detail)
    this.setData({
      visitDate: wx.utils.getStringFromTime(ev.detail),
      dateLayerVisible: false,
    })
  },
  async getHouseList() {
    const res = await wx.http.get('/house')
    // console.log(res)
    if (res.code !== 10000) return wx.utils.toast()
    this.setData({ houseList: res.data })
  },
  selectHouse(ev) {
    // console.log(ev.detail)
    const { id, name } = ev.detail
    this.setData({
      houseInfo: name,
      houseId: id,
    })
  },
  openHouseLayer() {
    this.setData({ houseLayerVisible: true })
  },
  closeHouseLayer() {
    this.setData({ houseLayerVisible: false })
  },
  openDateLayer() {
    this.setData({ dateLayerVisible: true })
  },
  closeDateLayer() {
    this.setData({ dateLayerVisible: false })
  },
  async goPassport() {
    // 完成新增访客 => 通行证页面
    // wx.reLaunch({
    //   url: '/visitor_pkg/pages/passport/index',
    // })

    // 此处省略校验
    const { houseId, name, gender, mobile, visitDate } = this.data
    const res = await wx.http.post('/visitor', {
      houseId,
      name,
      gender,
      mobile,
      visitDate,
    })
    // console.log(res)
    if (res.code !== 10000) return wx.utils.toast()

    // 新增成功 跳转到当前访客邀请的通行证页面 且带id
    wx.redirectTo({
      url: '/visitor_pkg/pages/passport/index?id=' + res.data.id,
    })
  },
})
